package com.sky.controller.admin;

import com.sky.config.EmployeeLoginVOBuilder;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理", description = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        /**
         * 构建者设计模式
         *    如果我们创建一个类的对象，而这个对象他的属性很多(超过4个属性)那么就可以用构建者设计模式来优化我们的代码
         * 自己实现构建者设计模式：
         *    1. 创建一个Bulider类
         *    2. 定义一个属性(EmployeeLoginVO)
         *    3. builder定义一个builder的方法
         *    4. 定义各种赋值的方法
         *      .id(employee.getId())
         *       .userName(employee.getUsername())
         *       .name(employee.getName())
         *       .token(token)
         *    5. 定义一个build方法返回被封装好的vo对象
         */
       /* EmployeeLoginVO vo = new EmployeeLoginVO();
        vo.setId(1L);
        vo.setName("");
        vo.setToken("");
        vo.setUserName("");

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();*/

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVOBuilder
                .bulider()
                .basic(employee.getUsername(), employee.getName())
                .more(employee.getId(), token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     */

    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工,{}", employeeDTO);
        employeeService.save(employeeDTO);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO pageQueryDto){
        log.info("员工分页查询,{}", pageQueryDto);
        PageResult pageResult = employeeService.pageQuery(pageQueryDto);

        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("状态更新")
    public Result updateStatus(@PathVariable Integer status, Long id){
        log.info("状态更新,{},{}", status, id);
        employeeService.update(status, id);

        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<EmployeeVO> getById(@PathVariable Long id){
        log.info("根据id查询员工,{}", id);

        return Result.success(employeeService.getById(id));
    }

    @PutMapping
    @ApiOperation("更新员工")
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("更新员工,{}", employeeDTO);
        employeeService.updateEmployee(employeeDTO);

        return Result.success();
    }
}
